using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Troschuetz.Random;

namespace VirtualRink.Library
{
    public static  class Randomizer
    {
        static Random random;
        static NormalDistribution attributeDistribution;
        static NormalDistribution potentialDistribution;
        static NormalDistribution heightDistribution;
        static NormalDistribution weightDistribution;
        static NormalDistribution ageDistribution;
        static NormalDistribution anyAgeDistribution;
        static NormalDistribution normalDistribution;
        static string lastPosition = "G";
        static int goalieSkipCount = 0;

        static Randomizer()
        {
            random = new Random();
            attributeDistribution = new NormalDistribution();
			attributeDistribution.Mu = 50;
			attributeDistribution.Sigma = 15;

            potentialDistribution = new NormalDistribution();
            potentialDistribution.Mu = 50;
            potentialDistribution.Sigma = 23;

            heightDistribution = new NormalDistribution();
            heightDistribution.Mu = 72;
            heightDistribution.Sigma = 4;

            weightDistribution = new NormalDistribution();
            weightDistribution.Mu = 190;
            weightDistribution.Sigma = 10;

            ageDistribution = new NormalDistribution();
            ageDistribution.Mu = 18;
            ageDistribution.Sigma = 1;

            anyAgeDistribution = new NormalDistribution();
            anyAgeDistribution.Mu = 26;
            anyAgeDistribution.Sigma = 5;

            normalDistribution = new NormalDistribution();
        }

        public static int Next(int min, int max)
        {
            Thread.Sleep(1);
            return random.Next(min, max);
        }

        public static string NextPosition()
        {
            if(lastPosition == Constants.GoalieAbbreviation)
            {
                lastPosition = Constants.CenterAbbreviation;
                return lastPosition;
            }
            if(lastPosition == Constants.CenterAbbreviation)
            {
                lastPosition = Constants.LeftWingAbbreviation;
                return lastPosition;
            }
            if(lastPosition == Constants.LeftWingAbbreviation)
            {
                lastPosition = Constants.RightWingAbbreviation;
                return lastPosition;
            }
            if(lastPosition == Constants.RightWingAbbreviation)
            {
                lastPosition = Constants.DefenseAbbreviation;
                return lastPosition;
            }
            if(lastPosition == Constants.DefenseAbbreviation)
            {
                if(goalieSkipCount > 2)
                {
                    lastPosition = Constants.GoalieAbbreviation;
                    goalieSkipCount = 0;
                }
                else
                {
                    lastPosition = Constants.CenterAbbreviation;
                    goalieSkipCount = goalieSkipCount + 1;
                }
                return lastPosition;
            }

            return Constants.CenterAbbreviation;
        }

        public static int NextAttributeValue()
        {
            int result= Convert.ToInt32(attributeDistribution.NextDouble());
            if(result < 1)
            {
                return 1;
            }
            else if(result > 100)
            {
                return 100;
            }
            return result;
        }

        public static int NextPotentialValue()
        {
            int result = Convert.ToInt32(potentialDistribution.NextDouble());
            if(result < 1)
            {
                return 1;
            }
            else if(result > 100)
            {
                return 100;
            }
            return result;
        }

        public static int NextHeight()
        {
            return Convert.ToInt32(heightDistribution.NextDouble());
        }

        public static int NextWeight()
        {
            return Convert.ToInt32(weightDistribution.NextDouble());
        }

        public static int NextAge()
        {
            return Convert.ToInt32(ageDistribution.NextDouble());
        }

        public static int NextAnyAge()
        {
            return Convert.ToInt32(anyAgeDistribution.NextDouble());
        }

        public static int NextNormal(double mu, double sigma)
        {
            normalDistribution.Mu = mu;
            normalDistribution.Sigma = sigma;
            return Convert.ToInt32(normalDistribution.NextDouble());
        }
    }
}
